home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / javax / swing / JButton.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  7.3 KB  |  258 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)JButton.java    1.72 98/08/28
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package javax.swing;
  15.  
  16. import java.util.EventListener;
  17.  
  18. import java.awt.*;
  19. import java.awt.event.*;
  20. import java.awt.image.*;
  21.  
  22. import javax.swing.plaf.*;
  23. import javax.swing.event.*;
  24. import javax.accessibility.*;
  25.  
  26. import java.io.ObjectOutputStream;
  27. import java.io.ObjectInputStream;
  28. import java.io.IOException;
  29.  
  30.  
  31. /**
  32.  * An implementation of a "push" button.
  33.  * <p>
  34.  * To create a set of mutually exclusive buttons, create a {@link ButtonGroup} object and
  35.  * use its <code>add</code> method to include the JButton objects in the group.
  36.  * <p>
  37.  * See <a href="http://java.sun.com/docs/books/tutorial/ui/swing/button.html">How to Use Buttons</a>
  38.  * in <a href="http://java.sun.com/Series/Tutorial/index.html"><em>The Java Tutorial</em></a>
  39.  * for further documentation.
  40.  * <p>
  41.  * For the keyboard keys used by this component in the standard Look and
  42.  * Feel (L&F) renditions, see the
  43.  * <a href="doc-files/Key-Index.html#JButton">JButton</a> key assignments.
  44.  * <p>
  45.  * <strong>Warning:</strong>
  46.  * Serialized objects of this class will not be compatible with 
  47.  * future Swing releases.  The current serialization support is appropriate
  48.  * for short term storage or RMI between applications running the same
  49.  * version of Swing.  A future release of Swing will provide support for
  50.  * long term persistence.
  51.  *
  52.  * @beaninfo
  53.  *   attribute: isContainer false
  54.  *
  55.  * @version 1.72 08/28/98
  56.  * @author Jeff Dinkins
  57.  * @see ButtonGroup
  58.  */
  59. public class JButton extends AbstractButton implements Accessible {
  60.  
  61.     /**
  62.      * @see #getUIClassID
  63.      * @see #readObject
  64.      */
  65.     private static final String uiClassID = "ButtonUI";
  66.  
  67.     private boolean defaultCapable = true;
  68.  
  69.     /**
  70.      * Creates a button with no set text or icon.
  71.      */
  72.     public JButton() {
  73.         this(null, null);
  74.     }
  75.     
  76.     /**
  77.      * Creates a button with an icon.
  78.      *
  79.      * @param icon  the Icon image to display on the button
  80.      */
  81.     public JButton(Icon icon) {
  82.         this(null, icon);
  83.     }
  84.     
  85.     /**
  86.      * Creates a button with text.
  87.      *
  88.      * @param text  the text of the button
  89.      */
  90.     public JButton(String text) {
  91.         this(text, null);
  92.     }
  93.     
  94.     /**
  95.      * Creates a button with initial text and an icon.
  96.      *
  97.      * @param text  the text of the button.
  98.      * @param icon  the Icon image to display on the button
  99.      */
  100.     public JButton(String text, Icon icon) {
  101.         // Create the model
  102.         setModel(new DefaultButtonModel());
  103.  
  104.         // initialize
  105.         init(text, icon);
  106.     }
  107.  
  108.     /**
  109.      * Notification from the UIFactory that the L&F
  110.      * has changed. 
  111.      *
  112.      * @see JComponent#updateUI
  113.      */
  114.     public void updateUI() {
  115.         setUI((ButtonUI)UIManager.getUI(this));
  116.     }
  117.     
  118.  
  119.     /**
  120.      * Returns a string that specifies the name of the L&F class
  121.      * that renders this component.
  122.      *
  123.      * @return "ButtonUI"
  124.      * @see JComponent#getUIClassID
  125.      * @see UIDefaults#getUI
  126.      * @beaninfo
  127.      *        expert: true
  128.      *   description: A string that specifies the name of the L&F class.
  129.      */
  130.     public String getUIClassID() {
  131.         return uiClassID;
  132.     }
  133.  
  134.  
  135.     /**
  136.      * Returns whether or not this button is the default button
  137.      * on the RootPane.
  138.      *
  139.      * @return "boolean"
  140.      * @see JRootPane#setDefaultButton
  141.      * @beaninfo 
  142.      *  description: Whether or not this button is the default button
  143.      */
  144.     public boolean isDefaultButton() {
  145.         JRootPane root = SwingUtilities.getRootPane(this);
  146.         if (root != null) {
  147.             return root.getDefaultButton() == this;
  148.         }
  149.         return false;
  150.     }
  151.  
  152.     /**
  153.      * Returns whether or not this button is capable of being
  154.      * the default button on the RootPane.
  155.      *
  156.      * @return "boolean"
  157.      * @see #setDefaultCapable
  158.      * @see #isDefaultButton
  159.      * @see JRootPane#setDefaultButton
  160.      */
  161.     public boolean isDefaultCapable() {
  162.         return defaultCapable;
  163.     }
  164.  
  165.     /**
  166.      * Sets whether or not this button is capable of being
  167.      * the default button on the RootPane.
  168.      *
  169.      * @return "boolean"
  170.      * @see #isDefaultCapable
  171.      * @beaninfo 
  172.      *        bound: true
  173.      *    attribute: visualUpdate true
  174.      *  description: Whether or not this button can be the default button
  175.      */
  176.     public void setDefaultCapable(boolean defaultCapable) {
  177.         boolean oldDefaultCapable = this.defaultCapable;
  178.         this.defaultCapable = defaultCapable;
  179.         firePropertyChange("defaultCapable", oldDefaultCapable, defaultCapable);
  180.     }
  181.  
  182.  
  183.     /** 
  184.      * See readObject() and writeObject() in JComponent for more 
  185.      * information about serialization in Swing.
  186.      */
  187.     private void writeObject(ObjectOutputStream s) throws IOException {
  188.         s.defaultWriteObject();
  189.     if ((ui != null) && (getUIClassID().equals(uiClassID))) {
  190.         ui.installUI(this);
  191.     }
  192.     }
  193.  
  194.  
  195.     /**
  196.      * Returns a string representation of this JButton. This method 
  197.      * is intended to be used only for debugging purposes, and the 
  198.      * content and format of the returned string may vary between      
  199.      * implementations. The returned string may be empty but may not 
  200.      * be <code>null</code>.
  201.      * <P>
  202.      * Overriding paramString() to provide information about the
  203.      * specific new aspects of the JFC components.
  204.      * 
  205.      * @return  a string representation of this JButton.
  206.      */
  207.     protected String paramString() {
  208.     String defaultCapableString = (defaultCapable ? "true" : "false");
  209.     
  210.     return super.paramString() +
  211.         ",defaultCapable=" + defaultCapableString;
  212.     }
  213.  
  214.  
  215. /////////////////
  216. // Accessibility support
  217. ////////////////
  218.  
  219.     /**
  220.      * Get the AccessibleContext associated with this JComponent
  221.      *
  222.      * @return the AccessibleContext of this JComponent
  223.      * @beaninfo
  224.      *       expert: true
  225.      *  description: The AccessibleContext associated with this Button.
  226.      */
  227.     public AccessibleContext getAccessibleContext() {
  228.         if (accessibleContext == null) {
  229.             accessibleContext = new AccessibleJButton();
  230.         }
  231.         return accessibleContext;
  232.     }
  233.  
  234.     /**
  235.      * The class used to obtain the accessible role for this object.
  236.      * <p>
  237.      * <strong>Warning:</strong>
  238.      * Serialized objects of this class will not be compatible with
  239.      * future Swing releases.  The current serialization support is appropriate
  240.      * for short term storage or RMI between applications running the same
  241.      * version of Swing.  A future release of Swing will provide support for
  242.      * long term persistence.
  243.      */
  244.     protected class AccessibleJButton extends AccessibleAbstractButton {
  245.     
  246.         /**
  247.          * Get the role of this object.
  248.          *
  249.          * @return an instance of AccessibleRole describing the role of the 
  250.          * object
  251.          * @see AccessibleRole
  252.          */
  253.         public AccessibleRole getAccessibleRole() {
  254.             return AccessibleRole.PUSH_BUTTON;
  255.         }
  256.     } // inner class AccessibleJButton
  257. }
  258.